Skip to content

Fix range search bugs#70

Merged
declanvk merged 4 commits into
mainfrom
fix-range-search-bugs
May 2, 2026
Merged

Fix range search bugs#70
declanvk merged 4 commits into
mainfrom
fix-range-search-bugs

Conversation

@declanvk
Copy link
Copy Markdown
Owner

@declanvk declanvk commented May 2, 2026

  • Improve tree API fuzzer with new range action
  • Switch unchecked assertions to normal asserts
  • Fix bug in sorted inner node range lookup
  • Fix buggy bounds in tree map range iterator

declanvk added 4 commits May 1, 2026 20:57
**Description**
 - Added new action to the fuzzer input which will perform a range
   lookup on the tree and compare it to a similar lookup on the oracle
   implementation
 - Add some comments around future fuzzer improvements
 - Turn on debug assertions for the fuzzer

**Motivation**
More coverage of the API by the fuzzer the better, especially because
it helps uncover lots of corner cases using an oracle. The debug
assertions help in the same vein because they can catch internal
inconsistencies.

**Testing Done**
I ran the fuzzer and founds some issues, those are addressed in the
next couple commits.
**Description**
Switch a couple of unchecked assertions to normal assertions because
they had no safety justification that worked locally. The assertions
weren't currently in danger of causing an issue, but future changes
could have put them in violation without more explicit docs on the
containing function. Better to upgrade to panic for now.

**Testing Done**
Ran fuzzer and existing unit tests.
**Description**
The `InnerNodeSorted::range` function had bugs when translating the
lookup bound and search results into array bounds. Specifically the issue
was in handling `Bound::{Included, Excluded}(WritePoint::Last(_))`, there
was some adjustments needed when using the bound as a start vs end bound.

**Motivation**
These bugs were not exercised in the normal lookup process, only in the
`TreeMap::range` iterator lookup process, which had a bunch of other bugs
unrelated to these.

**Testing Done**
Added some more unit tests, ran through fuzzer.
**Description**
This commit fixes bugs across all conditions of the range iterator
internals. All the bugs were concentrated in the code which finds the
correct start or end leaf node for the iterator when the range search
ends on an internal node. Specifically:
 - For the "missing child" termination reason (which happens when the
   lookup stops because there is no inner node child for the next key
   byte), there was an incorrect early return if the child node lookup
   returned `None`. Instead it should have turned that `None` case into
   a lookup on the minimum subtree child or maximum subtree child
   depending on the case.
 - The "insufficient bytes" and "prefix mismatch" cases were clumped
   together incorrectly. Splitting out "insufficient bytes" case
   simplified its logic. Similarly, clarified the "prefix mismatch" case,
   I think there was also a bug in there with the different cases of
   min/max and next/previous.
 - Switched from using the optimistic/pessimistic lookup optimization to
   just looking up the full prefix of an inner node when needed for
   range search. The optimistic case of the optimization was a problem
   for correctly classifying something as "insufficient bytes" versus
   "prefix mismatch", since implicit bytes make it impossible to tell
   which was the real cause.

I added a bunch of comments around the range leaf node lookup logic, I
hope I got it right this time. Also cleaned up some now dead code.

**Motivation**
When I added the oracle fuzzer for the range iterator in a previous
commit, it kept turning up regressions. I think I went through 3
different cases, rewriting the range lookup code, before I realized I
needed to remove the optimistic/pessimistic optimization from this code
path.

**Testing Done**
Added a bunch of unit tests, all pass. Fuzzer also ran for one hour
without finding any new issues.
@declanvk declanvk merged commit c9a91ff into main May 2, 2026
4 checks passed
@declanvk declanvk deleted the fix-range-search-bugs branch May 2, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant